// pages/category/index.js
import { request } from "../../request/index.js"
import regeneratorRuntime from "../../lib/runtime/runtime.js"

Page({

    /**
     * 页面的初始数据
     */
    data: {
        leftMenuList: [],
        rightContent: [],
        currentIndex: 0,
        scrollTop: 0

    },
    cates: [],
    /**
     * 左侧菜单点击事件
     * */
    handelItemTap(e) {
        const { index } = e.currentTarget.dataset;
        let rightContent = this.cates[index].children;
        this.setData({
            currentIndex: index,
            rightContent,
            scrollTop: 0
        });

    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        const cates = wx.getStorageSync("cates");
        if (!cates) {
            this.getCates();
        } else {
            if (Date.now() - cates.time > 1000 * 10) {
                this.getCates();
            } else {
                this.cates = cates.data;
                let leftMenuList = this.cates.map(v => v.cat_name);
                this.setData({
                    leftMenuList
                });
                let rightContent = this.cates[0].children;
                this.setData({
                    rightContent
                });
            }
        }
    },

    // 获取分类数据
    async getCates() {
        // request({
        //     url: "/categories"
        // })
        //     .then(res => {
        //         this.cates = res.data.message;
        //         wx.setStorageSync("cates", { time: Date.now(), data: this.cates });
        //         let leftMenuList = this.cates.map(v => v.cat_name);
        //         this.setData({
        //             leftMenuList
        //         });
        //         let rightContent = this.cates[0].children;
        //         this.setData({
        //             rightContent
        //         });

        //     })
        const res = await request({
            url: "/categories"
        });
        this.cates = res;
        wx.setStorageSync("cates", { time: Date.now(), data: this.cates });
        let leftMenuList = this.cates.map(v => v.cat_name);
        this.setData({
            leftMenuList
        });
        let rightContent = this.cates[0].children;
        this.setData({
            rightContent
        });
    }
})